<?php
//Set up the system
ob_start();
require('config.include.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pure Admin</title>
<link rel="stylesheet" href="login.css" />
</head>

<body>
<div id="container">
<div id="login">
<?php
if($_p->is_logged_in()) {
	if($_REQUEST['redirect'])
		$redirect= $_REQUEST['redirect'];
	else
		$redirect= 'dashboard.php';
	echo('<script type="text/javascript">window.location="'.$redirect.'"</script><h2>Welcome, '.$_p->tb_users->getFieldValue($_SESSION['auth_id'],'username').'</h2><br />
		<a href="dashboard.php">Go to dashboard</a>');
}
elseif(!isset($_POST['username'])||!isset($_POST['password'])) {
	if($_REQUEST['redirect'])
		$redirect= $_REQUEST['redirect'];
	else
		$redirect= 'dashboard.php';
	echo('<h2><span><a href="../">&lsaquo; back</a></span>Login</h2>
	<form action="index.php" method="post">
		<strong>Username: </strong><input name="username" type="text" /><br />
		<strong style="margin-left:-5px">Password: </strong><input name="password" type="password" /><br /><br />
			<input type="checkbox" id="rememberme" name="rememberme" value="on"/><label for="rememberme">Remember Me</label><br />
		<input type="submit" id="submit" align="right" value="Login" />
		<input type="hidden" name="redirect" value="'.$redirect.'" />
	</form>');
}
else {
	if($_p->log_in($_POST['username'],$_POST['password'])) {
		if(isset($_POST['rememberme']) && $_POST['rememberme']=="on"){
			setcookie('ui', $_p->usernameToId($_POST['username']).'-'.md5($_POST['password']), time()+31536000, "/");
		}
		echo('<script type="text/javascript">window.location="'.$_POST['redirect'].'"</script><h2>Logged in.</h2>
		<a href="dashboard.php">Go to dashboard</a>');
	}
	else {
		echo('<h2>Wrong username or password! Try again.</h2>
		<form action="index.php" method="post">		
			<strong>Username: </strong><input name="username" value="'.$_POST['username'].'" type="text" /><br />
			<strong style="margin-left:-5px">Password: </strong><input name="password" type="password" id="password"/><br /><br />
			<input type="checkbox" id="rememberme" name="rememberme" value="on"/><label for="rememberme">Remember Me</label><br />
			<input type="submit" id="submit" align="right" value="Login" />
		</form>');
	}
}
?>
</div>
</div>
</body>
</html>
